<?php
declare (strict_types = 1);

namespace app\admin\model\content;

use think\Model;

/**
 * @mixin \think\Model
 */
class ArticleCategory extends Model
{
    protected $pk = "article_category_id";

    /**
     * 获取信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id){
        return $this->where('article_category_id',$id)->find();
    }


    /**
     * 列表
     * @param $getData
     * @param $page
     * @param $limit
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page,$limit,$where = []){
        $sql = $this->where($where);
        if(isset($getData['title']) && $getData['title']) $sql = $sql->whereLike('title','%'.$getData['title'].'%');
        $count = $sql->count();
        $statusType = [1=>'显示',0=>'隐藏'];
        $data = $sql->page($page,$limit)->order('article_category_id','desc')->select()->each(function ($item)use($statusType){
            $item['status_name'] = $statusType[$item['is_status']];
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     *  添加数据
     * @param $addData
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData($addData){
        return $this->insertGetId([
            'title'=>$addData['title'],
            'pid'=>0,
            'mer_id'=>0,
            'info'=>$addData['info'],
            'image'=>$addData['image'],
            'sort'=>$addData['sort'],
            'is_status'=>$addData['is_status'],
            'remark'=>$addData['remark'],
            'type'=>isset($addData['type']) ? $addData['type'] : 1,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
        ]);
    }

    /**
     * 修改
     * @param $saveData
     * @return static
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editData($saveData){
        $updateData = [
            'title'=>$saveData['title'],
            'info'=>$saveData['info'],
            'image'=>$saveData['image'],
            'sort'=>$saveData['sort'],
            'is_status'=>$saveData['is_status'],
            'remark'=>$saveData['remark'],
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        return $this->where('article_category_id',$saveData['id'])->update($updateData);
    }

    /**
     * 删除
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function delData($id){
        $articleModel = new Article();
        $articleList =  $articleModel->getCategoryById($id);
        if(!$articleList || empty($articleList)){
            $result = $this->where('article_category_id',$id)->delete();
            if($result) return ['code'=>0,'msg'=>'删除成功'];
            return ['code'=>1,'msg'=>'删除失败，请稍后重试'];
        }else{
            return ['code'=>1,'msg'=>'请删除该类型下的文章'];
        }
    }

    /**
     * 获取指定内容
     * @param $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAllList($where,$field='*'){
        return $this->where($where)->field($field)->select()->toArray();
    }

}
